//
//  RadioCollectionViewCell.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/7.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "RadioCollectionViewCell.h"
#import "RadioTableViewCell.h"
#import "CFMacro.h"
#import "GXNetTool.h"
#import "GX_Button.h"
#import "RadioTableViewCell.h"
#import "RadioBannerTableViewCell.h"

// model
#import "RadioRootModel.h"
#import "Program_List_All.h"
#import "RadioBanners.h"

#import "GXCycleImages.h"
#import "DJDetailBannerViewController.h"
#import "DJDetailViewController.h"

#define kRadioTableViewCell @"RadioTableViewCell"
#define kRadioBannerTableViewCell @"RadioBannerTableViewCell"

@interface RadioCollectionViewCell () <UITableViewDataSource, UITableViewDelegate>
// 搜索视图
@property (weak, nonatomic) IBOutlet UIView *searchView;
// tableView
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UIViewController *vc;
@property (nonatomic, assign) BOOL isOpen;
@property (nonatomic, strong) NSIndexPath *selectIndex;

@end

@implementation RadioCollectionViewCell

#pragma mark - 点击排序按钮
- (IBAction)upToDateButtonClicked:(id)sender {
    NSLog(@"排序最新");
}
#pragma mark - 点击地区按钮
- (IBAction)locationButtonClicked:(id)sender {
    NSLog(@"地区广东");
}

#pragma mark - init
- (void)awakeFromNib {
    [super awakeFromNib];
    
    // tableView
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    // 注册正常cell
    kTalbleViewRegisterNib(@"RadioTableViewCell", kRadioTableViewCell);
    // 注册轮播图cell
    kTalbleViewRegisterNib(@"RadioBannerTableViewCell", kRadioBannerTableViewCell);
    
}

#pragma mark - 处理数据
- (void)handleData {
    // 电台页内容
    [GXNetTool GET:kRadioURL(0, 0, 0) andBody:nil andHeader:nil andResponse:GXJSON andSuccessBlock:^(id result) {
        self.root = [RadioRootModel modelWithDictionary:result];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}

#pragma mark - model setter
- (void)setRoot:(RadioRootModel *)root {
    _root = root;
    [self.tableView reloadData];
}

#pragma mark - tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_root.program_list_all[indexPath.row].banners) {
        RadioBannerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kRadioBannerTableViewCell];
        if (_root) {
            cell.banners = _root.program_list_all[indexPath.row].banners;
            cell.cycleView.indexPath = ^(NSIndexPath *index) {
                switch (cell.banners[index.row - 1].banner_type) {
                    case 0: {
                        NSLog(@"type 0, 直接跳转播放页");
                    } break;
                    case 1: {
                        NSLog(@"type 1, 跳转节目详情页");
                        DJDetailBannerViewController *playerVC = [[DJDetailBannerViewController alloc] init];
                        [self.vc.navigationController pushViewController:playerVC animated:YES];
                    } break;
                    case 2: NSLog(@"type 2"); break;
                    case 3: {
                        NSLog(@"type 3, 跳转djDetailVC");
                        DJDetailViewController *djDetailVC = [[DJDetailViewController alloc] init];
                        [self.vc.navigationController pushViewController:djDetailVC animated:YES];
                    } break;
                        
                    default:break;
                }
            };
        }
        return cell;
    } else {
        RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kRadioTableViewCell];
        if (_root) {
            cell.programlist = _root.program_list_all[indexPath.row];
            cell.button = ^(GX_Button *button) {
                [self showMoreCellByButton:button indexPath:indexPath];

                
            };
        }
        return cell;
    }
}

- (void)showMoreCellByButton:(GX_Button *)button indexPath:(NSIndexPath *)indexPath {
    button.selected = !button.selected;
    //更改选中cell的状态 用以 刷新页面是进行折叠和展开
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    if (_selectIndex != nil && indexPath.row == _selectIndex.row) {
        _isOpen=!_isOpen;
        
    } else if (_selectIndex != nil && indexPath.row != _selectIndex.row) {
        indexPaths = [NSArray arrayWithObjects:indexPath, _selectIndex, nil];
        _isOpen = YES;
    }
    _selectIndex=indexPath;
    //刷新
    [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_selectIndex.row == indexPath.row && _selectIndex != nil) {
        if (_isOpen) {
            return 160;
        }
    }
    if (_root.program_list_all[indexPath.row].banners) {
        return 120;
    } else if (indexPath.row == 0) {
        return 120;
    } else {
        return 80;
    }
}

- (UIViewController *)vc {
    id responder = self.nextResponder;
    while (![responder isKindOfClass:[UIViewController class]] && responder != nil) {
        responder = [responder nextResponder];
    }
    return (UIViewController *)responder;
}

@end
